package com.study.forumsystemproject.interceptor;

import com.study.forumsystemproject.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * program: forum-system-project
 * <p>
 * description: 登录拦截器
 * <p>
 * author: xiaoxie
 * <p>
 * create: 2024-08-16 21:22
 *
 * @author 谢忠涵7*/
@Configuration
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    /**
     * Description: 前置拦截器
     * Param: * @param request
     * @param handler
     * return: true 进行 false 中断
     * Author: xiaoxie
     * Date: 21:23 2024/8/16
    */
    // 从配置⽂件中获取默认登录⻚的URL
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String userTokenHeader = request.getHeader("user_token_header");
        Claims claims = JwtUtil.verifyJwt(userTokenHeader);

        if (claims == null) {
            response.setStatus(401);
            log.error("用户未登录");
            return false;
        }
        return true;
    }
}
